package com.linkoog.devtools.http.toolwindow.workbenche.left.http_service;

import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiMethod;
import com.intellij.util.OpenSourceUtil;
import com.linkoog.devtools.http.toolwindow.workbenche.left.http_service.node.ClassTreeNode;
import com.linkoog.devtools.http.toolwindow.workbenche.left.http_service.node.MethodTreeNode;
import com.linkoog.devtools.http.toolwindow.workbenche.left.http_service.node.ModuleTreeNode;
import com.linkoog.devtools.http.toolwindow.workbenche.left.http_service.node.PackageTreeNode;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;

public class HttpServiceTreeMenuProvider {

    private final Project project;
    private final JTree myTree;

    public HttpServiceTreeMenuProvider(Project project, JTree myTree) {
        this.project = project;
        this.myTree = myTree;
    }

    public JPopupMenu createNodePopupMenu(MouseEvent mouseEvent, TreeNode treeNode) {
        if (treeNode instanceof ModuleTreeNode moduleTreeNode){
            return createHttpModuleNodePopupMenu(moduleTreeNode);
        } else if (treeNode instanceof PackageTreeNode packageTreeNode) {
            return createHttpPackageNodePopupMenu(packageTreeNode);
        } else if (treeNode instanceof ClassTreeNode classTreeNode) {
            return createHttpClassNodePopupMenu(classTreeNode);
        } else if (treeNode instanceof MethodTreeNode methodTreeNode) {
            return createHttpMethodNodePopupMenu(methodTreeNode);
        }
        return null;
    }


    /**
     * httpModule节点的右键菜单
     */
    @NotNull
    private JPopupMenu createHttpModuleNodePopupMenu(ModuleTreeNode moduleTreeNode) {
        JPopupMenu popupMenu = new JPopupMenu();

        JMenuItem refreshAction = new JMenuItem("Refresh");
        popupMenu.add(refreshAction);
        refreshAction.addActionListener(e -> {

        });

        return popupMenu;
    }

    @NotNull
    private JPopupMenu createHttpPackageNodePopupMenu(PackageTreeNode packageTreeNode) {
        JPopupMenu popupMenu = new JPopupMenu();

        JMenuItem refreshAction = new JMenuItem("Refresh");
        popupMenu.add(refreshAction);
        refreshAction.addActionListener(e -> {

        });

        return popupMenu;
    }

    /**
     * httpClass节点的右键菜单
     */
    @NotNull
    private JPopupMenu createHttpClassNodePopupMenu(ClassTreeNode classTreeNode) {
        JPopupMenu popupMenu = new JPopupMenu();

        JMenuItem refreshAction = new JMenuItem("Refresh");
        popupMenu.add(refreshAction);
        refreshAction.addActionListener(e -> {

        });


        JMenuItem navigateAction = new JMenuItem("Jump to Code");
        popupMenu.add(navigateAction);
        navigateAction.addActionListener(e -> {
            final PsiClass psiClass = classTreeNode.getHttpServiceClass().getPsiElement();
            OpenSourceUtil.navigate(true, psiClass);
        });

        return popupMenu;
    }

    /**
     * httpMethod节点的右键菜单
     */
    @NotNull
    private JPopupMenu createHttpMethodNodePopupMenu(MethodTreeNode methodTreeNode) {
        JPopupMenu popupMenu = new JPopupMenu();

        JMenuItem navigateAction = new JMenuItem("Jump to Code");
        popupMenu.add(navigateAction);
        navigateAction.addActionListener(e -> {
            final PsiMethod psiMethod = methodTreeNode.getHttpServiceMethod().getPsiElement();
            OpenSourceUtil.navigate(true, psiMethod);
        });

        /*JMenuItem copyCurlAction = new JMenuItem("Copy cURL");
        popupMenu.add(copyCurlAction);
        copyCurlAction.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) httpServiceTree.getLastSelectedPathComponent();

                if (selectedNode.getUserObject() instanceof RestMethodNodeData) {
                    RestMethodNodeData serviceNode = (RestMethodNodeData) selectedNode.getUserObject();
                    String partialUrl = serviceNode.getHttpUrl();

                    String fullUrl =    "";                      //copyRestUrlUtil.getFullUrl(partialUrl, serviceNode);

                    StringBuilder curl = new StringBuilder("curl -X ");
                    curl.append(serviceNode.getMethod());
                    curl.append(" " + fullUrl);

                    CopyPasteManager.getInstance().setContents(new StringSelection(curl.toString()));
                }
            }
        });

        JMenuItem copyRestUrlAction = new JMenuItem("Copy REST Url");
        popupMenu.add(copyRestUrlAction);
        copyRestUrlAction.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) httpServiceTree.getLastSelectedPathComponent();

                if (selectedNode.getUserObject() instanceof RestMethodNodeData) {
                    RestMethodNodeData serviceNode = (RestMethodNodeData) selectedNode.getUserObject();
                    String partialUrl = ((RestMethodNodeData) selectedNode.getUserObject()).getHttpUrl();

                    String fullUrl =    "";                      //copyRestUrlUtil.getFullUrl(partialUrl, serviceNode);
                    CopyPasteManager.getInstance().setContents(new StringSelection(fullUrl));
                }
            }
        });*/




        JMenuItem generateMarkdownDocAction = new JMenuItem("Generate Markdown");
        popupMenu.add(generateMarkdownDocAction);
        generateMarkdownDocAction.addActionListener(event -> {

        });

        return popupMenu;
    }

}
